package com.mt.roll.report.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.*;
import com.mt.roll.core.dal.service.*;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.report.mapper.ReportSubstrateVoMapper;
import com.mt.roll.report.params.ReportSubstrateParam;
import com.mt.roll.report.params.ReportSubstrateQueryParam;
import com.mt.roll.report.service.ReportSubstrateSerice;
import com.mt.roll.report.vo.ReportStorageBoxVo;
import com.mt.roll.report.vo.ReportSubstrateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: yulianchen
 * @Date: 2022/3/4 14:52
 * @Version 1.0
 */
@Service
@Transactional
public class ReportSubstrateSericeImpl extends BaseService implements ReportSubstrateSerice {

//    @Resource
//    private StorageInfoDetServiceBiz storageInfoDetServiceBiz;
    @Resource
    private StorageBoxCircInfoServiceBiz storageBoxCircInfoServiceBiz;
    @Resource
    private ReportSubstrateVoMapper reportSubstrateVoMapper;
    @Resource
    private LocationStorageSubServiceBiz locationStorageSubServiceBiz;

    /**
     * 获取存储箱对应基板列表
     *
     * @param reportSubstrateQueryParam    查询参数：存储箱编码
     * @return 存储箱对应基板报表
     */
    @Override
    public R<List<ReportSubstrateVo>> list(ReportSubstrateQueryParam reportSubstrateQueryParam) {
        List<ReportSubstrateVo> reportSubstrateVoList = reportSubstrateVoMapper.getReportSubstrateVoList(reportSubstrateQueryParam.getLocationStorageSubID());
        return successful(reportSubstrateVoList);
    }


    /**
     * 获取存储箱对应基板分页列表
     *
     * @param reportSubstrateQueryParam    查询参数：存储箱编码
     * @return 存储箱对应基板分页报表
     */
    @Override
    public R<PageVo<ReportSubstrateVo>> page(ReportSubstrateQueryParam reportSubstrateQueryParam) {
        IPage<ReportSubstrateVo> reportSubstrateVoList = reportSubstrateVoMapper.getReportSubstrateVoList(new Page<>(reportSubstrateQueryParam.getCurrent(), reportSubstrateQueryParam.getSize()), reportSubstrateQueryParam.getLocationStorageSubID());
        return successful(new PageVo<>(reportSubstrateVoList,ReportSubstrateVo.class));
    }

    /**
     * 更新基板信息
     * @param reportSubstrateParam 基板参数
     * @return 主键
     */
    @Override
    public R<IdVo> update(ReportSubstrateParam reportSubstrateParam) {
        LocationStorageSub locationStorageSub = locationStorageSubServiceBiz.getById(reportSubstrateParam.getId());
//        QueryWrapper<LocationStorageSub> wrapper=new QueryWrapper<>();
//        wrapper.eq(LocationStorageSub.SUB_CODE,reportSubstrateParam.getSubstrateCode());
//        //基板编码不能重复
//        Assert.ge(1,storageInfoDetServiceBiz.count(wrapper),"基板编码已存在");
        LocationStorageSub updateLocationStorageSub=new LocationStorageSub();
        BeanUtils.copyProperties(reportSubstrateParam,updateLocationStorageSub);
        setUpdateCommonField(updateLocationStorageSub);
        locationStorageSubServiceBiz.updateById(updateLocationStorageSub);
        return successful(IdVo.builder().id(locationStorageSub.getId()).build());
    }

    /**
     * 删除基板
     *
     * @param idParam 主键参数
     * @return 主键
     */
    @Override
    public R<IdVo> delete(IdParam idParam) {
        LocationStorageSub locationStorageSub = locationStorageSubServiceBiz.getById(idParam.getId());
        if (locationStorageSub==null){
            return failed("基板不存在");
        }
        locationStorageSubServiceBiz.removeById(idParam.getId());
        //当前对应基板为0时，基板状态改为空
//        QueryWrapper<StorageInfoDet> wrapper=new QueryWrapper<>();
//        wrapper.eq(StorageInfoDet.STORAGE_CODE,storageInfoDet.getStorageCode());
//        //检查是否还存在对应基板
//        if (storageInfoDetServiceBiz.count(wrapper)==0){
//            StorageBoxCircInfo storageBoxCircInfo = new StorageBoxCircInfo();
//            storageBoxCircInfo.setId(storageInfoDet.getStorageBoxId());
//            storageBoxCircInfo.setIsEmpty(0);
//            storageBoxCircInfoServiceBiz.updateById(storageBoxCircInfo);
//        }
        return successful(IdVo.builder().id(locationStorageSub.getId()).build());
    }
}
